package gameengine;

import java.io.*;
import javax.sound.sampled.*;

/**
 * Classe que toca soms
 * @author Victor
 */
public class MediaPlayer extends Thread
{
	private static MediaPlayer _instance = null;
	
	private String filename;
	private Position curPosition;
	private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb

	/**
	 * Posições em que o som tocará
	 * @author Victor
	 */
	enum Position
	{

		LEFT, RIGHT, NORMAL
	};

	/**
	 * Retorna a instância da classe
	 * @author Victor
	 */
	public static MediaPlayer GetInstance()
	{
		if (_instance == null)
		{
			_instance = new MediaPlayer();
		}

		return _instance;
	}

	/**
	 * Toca o som
	 * @author Victor
	 */
	public void Play(String wavfile)
	{
		filename = wavfile;
		curPosition = Position.NORMAL;
		this.start();
	}

	@Override
	/**
	 * Toca o som
	 * @owner Victor
	 */
	public void run()
	{
		File soundFile = new File(filename);
		if (!soundFile.exists())
		{
			System.err.println("Wave file not found: " + filename);
			return;
		}

		AudioInputStream audioInputStream = null;
		try
		{
			audioInputStream = AudioSystem.getAudioInputStream(soundFile);
		}
		catch (UnsupportedAudioFileException e1)
		{
			e1.printStackTrace();
			return;
		}
		catch (IOException e1)
		{
			e1.printStackTrace();
			return;
		}

		AudioFormat format = audioInputStream.getFormat();
		SourceDataLine auline = null;
		DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

		try
		{
			auline = (SourceDataLine) AudioSystem.getLine(info);
			auline.open(format);
		}
		catch (LineUnavailableException e)
		{
			e.printStackTrace();
			return;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return;
		}

		if (auline.isControlSupported(FloatControl.Type.PAN))
		{
			FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);
			if (curPosition == Position.RIGHT)
			{
				pan.setValue(1.0f);
			}
			else if (curPosition == Position.LEFT)
			{
				pan.setValue(-1.0f);
			}
		}

		auline.start();
		int nBytesRead = 0;
		byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

		try
		{
			while (nBytesRead != -1)
			{
				nBytesRead = audioInputStream.read(abData, 0, abData.length);
				if (nBytesRead >= 0)
				{
					auline.write(abData, 0, nBytesRead);
				}
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return;
		}
		finally
		{
			auline.drain();
			auline.close();
		}
	}
}
